package com.shiguiwu.springmybatis.designpattern.dutychain.scene;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

/**
 * @description: 击鼓人
 * @author: stone
 * @date: Created by 2022/11/7 22:18
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.designpattern.dutychain.scene
 */
@Slf4j
public class DrumBeater {

    private Player header;

    //private Timer timer = new Timer();

    public DrumBeater(Player header) {
        this.header = header;
    }

    public DrumBeater() {
    }

    /**
     * 开始游戏
     */
    public void start() {
        Timer timer = new Timer();
        timer.schedule(new MyTask(this),1000L);

    }

    /**
     * 开始击鼓
     */
    private void startDrumBeat() {
        //开始传花
        new Thread(() -> {
            try {
                this.header.handle();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).start();



    }

    /**
     * 击鼓计数器
     */
    public static class MyTask extends TimerTask {

        /**
         * 行酒令最大次数
         */
        public static int max = 15;

        private DrumBeater drumBeater;

        public MyTask(DrumBeater drumBeater) {
            this.drumBeater = drumBeater;
        }

        @Override
        public void run() {
            log.info("击鼓开始 =======> ");
            int i = 0;
            try {
                drumBeater.startDrumBeat();
                while (i < 15) {
                    TimeUnit.SECONDS.sleep(RandomUtil.randomInt(0, 5));
                    Player.isStop = true;
                    i++;
                    log.error("这是第{}次击鼓 ====> ", i);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            finally {
                cancel();
            }

        }
    }


}
